<?php require_once('Connections/Insapeca.php'); ?>
<?php require_once('Connections/Insapeca_autor_x_material_cientifico.php'); ?>
<?php require_once('Connections/Insapeca_palavra_chave_x_material_cientifico.php'); ?>
<?php require_once('Connections/Insapeca_palavraChave.php'); ?>
<?php
//MX Widgets3 include
require_once('includes/wdg/WDG.php');
?>
<?php require_once('Connections/Insapeca_autor.php'); ?>
<?php
// Load the common classes
require_once('includes/common/KT_common.php');

// Load the tNG classes
require_once('includes/tng/tNG.inc.php');

// Load the KT_back class
require_once('includes/nxt/KT_back.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("");

// Make unified connection variable
$conn_Insapeca = new KT_connection($Insapeca, $database_Insapeca);

// Start trigger
$formValidation = new tNG_FormValidation();
$formValidation->addField("ds_titulo", true, "text", "", "", "", "Por favor insira um valor válido.");
$formValidation->addField("ds_tema", true, "text", "", "", "", "Por favor insira um valor válido.");
$formValidation->addField("ds_resumo", true, "text", "", "", "", "Por favor insira um valor válido.");
$formValidation->addField("id_tp_material_cientfico", true, "text", "", "", "", "Por favor insira um valor válido.");
$formValidation->addField("dir_arquivo_servidor", true, "text", "", "", "", "Por favor insira um valor válido.");
$tNGs->prepareValidation($formValidation);
// End trigger

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO autor (nm_autor) VALUES (%s)",
                       GetSQLValueString($_POST['ds_autor'], "text"));

  mysql_select_db($database_Insapeca, $Insapeca);
  $Result1 = mysql_query($insertSQL, $Insapeca) or die(mysql_error());
}

mysql_select_db($database_Insapeca_autor, $Insapeca_autor);
$query_popula_autor = "SELECT * FROM autor ORDER BY nm_autor ASC";
$popula_autor = mysql_query($query_popula_autor, $Insapeca_autor) or die(mysql_error());
$row_popula_autor = mysql_fetch_assoc($popula_autor);
$totalRows_popula_autor = mysql_num_rows($popula_autor);

mysql_select_db($database_Insapeca_palavraChave, $Insapeca_palavraChave);
$query_popula_palavraChave = "SELECT * FROM palavra_chave ORDER BY ds_palavra_chave ASC";
$popula_palavraChave = mysql_query($query_popula_palavraChave, $Insapeca_palavraChave) or die(mysql_error());
$row_popula_palavraChave = mysql_fetch_assoc($popula_palavraChave);
$totalRows_popula_palavraChave = mysql_num_rows($popula_palavraChave);

mysql_select_db($database_Insapeca_autor_x_material_cientifico, $Insapeca_autor_x_material_cientifico);
$query_autor_x_material_cientifico = "SELECT * FROM autor_x_material_cientifico";
$autor_x_material_cientifico = mysql_query($query_autor_x_material_cientifico, $Insapeca_autor_x_material_cientifico) or die(mysql_error());
$row_autor_x_material_cientifico = mysql_fetch_assoc($autor_x_material_cientifico);
$totalRows_autor_x_material_cientifico = mysql_num_rows($autor_x_material_cientifico);

mysql_select_db($database_Insapeca_palavra_chave_x_material_cientifico, $Insapeca_palavra_chave_x_material_cientifico);
$query_palavra_chave_x_material_cientifico = "SELECT * FROM palavra_chave_x_material_cientifico";
$palavra_chave_x_material_cientifico = mysql_query($query_palavra_chave_x_material_cientifico, $Insapeca_palavra_chave_x_material_cientifico) or die(mysql_error());
$row_palavra_chave_x_material_cientifico = mysql_fetch_assoc($palavra_chave_x_material_cientifico);
$totalRows_palavra_chave_x_material_cientifico = mysql_num_rows($palavra_chave_x_material_cientifico);

//start Trigger_FileDelete trigger
//remove this line if you want to edit the code by hand 
function Trigger_FileDelete(&$tNG) {
  $deleteObj = new tNG_FileDelete($tNG);
  $deleteObj->setFolder("Gerenciador_UploadDeArquivos/arquivos/");
  $deleteObj->setDbFieldName("dir_arquivo_servidor");
  return $deleteObj->Execute();
}
//end Trigger_FileDelete trigger

//start Trigger_FileUpload trigger
//remove this line if you want to edit the code by hand 
function Trigger_FileUpload(&$tNG) {
  $uploadObj = new tNG_FileUpload($tNG);
  $uploadObj->setFormFieldName("dir_arquivo_servidor");
  $uploadObj->setDbFieldName("dir_arquivo_servidor");
  $uploadObj->setFolder("arquivos/");
  $uploadObj->setMaxSize(10000);
  $uploadObj->setAllowedExtensions("pdf, doc, docx");
  $uploadObj->setRename("auto");
  return $uploadObj->Execute();
}
//end Trigger_FileUpload trigger

// Make an insert transaction instance
$ins_material_cientifico = new tNG_multipleInsert($conn_Insapeca);
$tNGs->addTransaction($ins_material_cientifico);
// Register triggers
$ins_material_cientifico->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$ins_material_cientifico->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_material_cientifico->registerTrigger("END", "Trigger_Default_Redirect", 99, "includes/nxt/back.php");
$ins_material_cientifico->registerTrigger("AFTER", "Trigger_FileUpload", 97);
// Add columns
$ins_material_cientifico->setTable("material_cientifico");
$ins_material_cientifico->addColumn("ds_titulo", "STRING_TYPE", "POST", "ds_titulo");
$ins_material_cientifico->addColumn("ds_tema", "STRING_TYPE", "POST", "ds_tema");
$ins_material_cientifico->addColumn("ds_resumo", "STRING_TYPE", "POST", "ds_resumo");
$ins_material_cientifico->addColumn("id_tp_material_cientfico", "STRING_TYPE", "POST", "id_tp_material_cientfico");
$ins_material_cientifico->addColumn("dir_arquivo_servidor", "FILE_TYPE", "FILES", "dir_arquivo_servidor");
$ins_material_cientifico->setPrimaryKey("id_material_cientifico", "NUMERIC_TYPE");

// Make an update transaction instance
$upd_material_cientifico = new tNG_multipleUpdate($conn_Insapeca);
$tNGs->addTransaction($upd_material_cientifico);
// Register triggers
$upd_material_cientifico->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
$upd_material_cientifico->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$upd_material_cientifico->registerTrigger("END", "Trigger_Default_Redirect", 99, "includes/nxt/back.php");
$upd_material_cientifico->registerTrigger("AFTER", "Trigger_FileUpload", 97);
// Add columns
$upd_material_cientifico->setTable("material_cientifico");
$upd_material_cientifico->addColumn("ds_titulo", "STRING_TYPE", "POST", "ds_titulo");
$upd_material_cientifico->addColumn("ds_tema", "STRING_TYPE", "POST", "ds_tema");
$upd_material_cientifico->addColumn("ds_resumo", "STRING_TYPE", "POST", "ds_resumo");
$upd_material_cientifico->addColumn("id_tp_material_cientfico", "STRING_TYPE", "POST", "id_tp_material_cientfico");
$upd_material_cientifico->addColumn("dir_arquivo_servidor", "FILE_TYPE", "FILES", "dir_arquivo_servidor");
$upd_material_cientifico->setPrimaryKey("id_material_cientifico", "NUMERIC_TYPE", "GET", "id_material_cientifico");

// Make an instance of the transaction object
$del_material_cientifico = new tNG_multipleDelete($conn_Insapeca);
$tNGs->addTransaction($del_material_cientifico);
// Register triggers
$del_material_cientifico->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Delete1");
$del_material_cientifico->registerTrigger("END", "Trigger_Default_Redirect", 99, "includes/nxt/back.php");
$del_material_cientifico->registerTrigger("AFTER", "Trigger_FileDelete", 98);
// Add columns
$del_material_cientifico->setTable("material_cientifico");
$del_material_cientifico->setPrimaryKey("id_material_cientifico", "NUMERIC_TYPE", "GET", "id_material_cientifico");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rsmaterial_cientifico = $tNGs->getRecordset("material_cientifico");
$row_rsmaterial_cientifico = mysql_fetch_assoc($rsmaterial_cientifico);
$totalRows_rsmaterial_cientifico = mysql_num_rows($rsmaterial_cientifico);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wdg="http://ns.adobe.com/addt">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Insapeca</title>
<link href="includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="includes/common/js/base.js" type="text/javascript"></script>
<script src="includes/common/js/utility.js" type="text/javascript"></script>
<script src="includes/skins/style.js" type="text/javascript"></script>
<?php echo $tNGs->displayValidationRules();?>
<script src="includes/nxt/scripts/form.js" type="text/javascript"></script>
<script src="includes/nxt/scripts/form.js.php" type="text/javascript"></script>
<script type="text/javascript">
$NXT_FORM_SETTINGS = {
  duplicate_buttons: false,
  show_as_grid: true,
  merge_down_value: true
}
</script>
<script type="text/javascript" src="includes/common/js/sigslot_core.js"></script>
<script type="text/javascript" src="includes/wdg/classes/MXWidgets.js"></script>
<script type="text/javascript" src="includes/wdg/classes/MXWidgets.js.php"></script>
<script type="text/javascript" src="includes/wdg/classes/JSRecordset.js"></script>
<script type="text/javascript" src="includes/wdg/classes/BaseListSorter.js"></script>
<script type="text/javascript" src="includes/wdg/classes/MenuMover.js"></script>
<?php
//begin JSRecordset
$jsObject_popula_autor = new WDG_JsRecordset("popula_autor");
echo $jsObject_popula_autor->getOutput();
//end JSRecordset
?>
<?php
//begin JSRecordset
$jsObject_popula_palavraChave = new WDG_JsRecordset("popula_palavraChave");
echo $jsObject_popula_palavraChave->getOutput();
//end JSRecordset
?>
</head>

<body>
<?php
	echo $tNGs->getErrorMsg();
?>
<div class="KT_tng">
  <h1>
    <?php 
// Show IF Conditional region1 
if (@$_GET['id_material_cientifico'] == "") {
?>
      <?php echo NXT_getResource("Inserir"); ?>
      <?php 
// else Conditional region1
} else { ?>
      <?php echo NXT_getResource("Atualizar"); ?>
      <?php } 
// endif Conditional region1
?>
    Material Cientfico </h1>
  <div class="KT_tngform">
    <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
      <?php $cnt1 = 0; ?>
      <?php do { ?>
        <?php $cnt1++; ?>
        <?php 
// Show IF Conditional region1 
if (@$totalRows_rsmaterial_cientifico > 1) {
?>
          <h2><?php echo NXT_getResource("Record_FH"); ?> <?php echo $cnt1; ?></h2>
          <?php } 
// endif Conditional region1
?>
        <table cellpadding="2" cellspacing="0" class="KT_tngtable">
          <tr>
          	<td height="23" class="KT_th"><label for="ds_autor_<?php echo $cnt1; ?>">Autor:</label></td>
            <td><input name="ds_autor_<?php echo $cnt1; ?>" id="ds_autor_<?php echo $cnt1; ?>" size="5" wdg:recordset="popula_autor" wdg:subtype="MenuMover" wdg:type="widget" wdg:displayfield="nm_autor" wdg:valuefield="id_autor" wdg:sortselector="no" /></td>
          </tr>
          <tr>
            <td class="KT_th"><label for="ds_titulo_<?php echo $cnt1; ?>">Título:</label></td>
            <td><p>
              <input type="text" name="ds_titulo_<?php echo $cnt1; ?>" id="ds_titulo_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rsmaterial_cientifico['ds_titulo']); ?>" size="80" maxlength="255" />
              <?php echo $tNGs->displayFieldHint("ds_titulo");?> <?php echo $tNGs->displayFieldError("material_cientifico", "ds_titulo", $cnt1); ?></p></td>
          </tr>
          <tr>
            <td class="KT_th"><label for="ds_tema_<?php echo $cnt1; ?>">Tema:</label></td>
            <td><input type="text" name="ds_tema_<?php echo $cnt1; ?>" id="ds_tema_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rsmaterial_cientifico['ds_tema']); ?>" size="80" maxlength="255" />
            <?php echo $tNGs->displayFieldHint("ds_tema");?> <?php echo $tNGs->displayFieldError("material_cientifico", "ds_tema", $cnt1); ?></td>
          </tr>
          <tr>
            <td class="KT_th"><div align="left">Palavra-Chave:</div></td>
            <td><input name="ds_palavraChave" id="ds_palavraChave" size="5" wdg:recordset="popula_palavraChave" wdg:subtype="MenuMover" wdg:type="widget" wdg:displayfield="ds_palavra_chave" wdg:valuefield="id_palavra_chave" wdg:sortselector="no" /></td>
          </tr>
          <tr>
            <td class="KT_th"><label for="ds_resumo_<?php echo $cnt1; ?>">Resumo:</label></td>
            <td><textarea name="ds_resumo_<?php echo $cnt1; ?>" id="ds_resumo_<?php echo $cnt1; ?>" cols="80" rows="5"><?php echo KT_escapeAttribute($row_rsmaterial_cientifico['ds_resumo']); ?></textarea>
            <?php echo $tNGs->displayFieldHint("ds_resumo");?> <?php echo $tNGs->displayFieldError("material_cientifico", "ds_resumo", $cnt1); ?></td>
          </tr>
          <tr>
            <td class="KT_th"><label for="id_tp_material_cientfico_<?php echo $cnt1; ?>_1">Tipo</label></td>
            <td><div>
              <input <?php if (!(strcmp(KT_escapeAttribute($row_rsmaterial_cientifico['id_tp_material_cientfico']),"0"))) {echo "@@checked@@";} ?> type="radio" name="id_tp_material_cientfico_<?php echo $cnt1; ?>" id="id_tp_material_cientfico_<?php echo $cnt1; ?>_1" value="0" />
              <label for="id_tp_material_cientfico_<?php echo $cnt1; ?>_1">Artigo Científico</label>
            </div>
              <div>
                <input <?php if (!(strcmp(KT_escapeAttribute($row_rsmaterial_cientifico['id_tp_material_cientfico']),"1"))) {echo "@@checked@@";} ?> type="radio" name="id_tp_material_cientfico_<?php echo $cnt1; ?>" id="id_tp_material_cientfico_<?php echo $cnt1; ?>_2" value="1" />
                <label for="id_tp_material_cientfico_<?php echo $cnt1; ?>_2">Dissertação</label>
              </div>
              <div>
                <input <?php if (!(strcmp(KT_escapeAttribute($row_rsmaterial_cientifico['id_tp_material_cientfico']),"2"))) {echo "@@checked@@";} ?> type="radio" name="id_tp_material_cientfico_<?php echo $cnt1; ?>" id="id_tp_material_cientfico_<?php echo $cnt1; ?>_3" value="2" />
                <label for="id_tp_material_cientfico_<?php echo $cnt1; ?>_3">Monografia</label>
              </div>
              <?php echo $tNGs->displayFieldError("material_cientifico", "id_tp_material_cientfico", $cnt1); ?></td>
          </tr>
          <tr>
            <td class="KT_th"><label for="dir_arquivo_servidor_<?php echo $cnt1; ?>">Arquivo:</label></td>
            <td><input name="dir_arquivo_servidor_<?php echo $cnt1; ?>" type="file" id="dir_arquivo_servidor_<?php echo $cnt1; ?>" size="80" />
            <?php echo $tNGs->displayFieldHint("dir_arquivo_servidor");?> <?php echo $tNGs->displayFieldError("material_cientifico", "dir_arquivo_servidor", $cnt1); ?></td>
          </tr>
        </table>
        <input type="hidden" name="kt_pk_material_cientifico_<?php echo $cnt1; ?>" class="id_field" value="<?php echo KT_escapeAttribute($row_rsmaterial_cientifico['kt_pk_material_cientifico']); ?>" />
        <?php } while ($row_rsmaterial_cientifico = mysql_fetch_assoc($rsmaterial_cientifico)); ?>
      <div class="KT_bottombuttons">
        <div>
          <?php 
      // Show IF Conditional region1
      if (@$_GET['id_material_cientifico'] == "") {
      ?>
          <input type="submit" name="KT_Insert1" id="KT_Insert1" value="<?php echo NXT_getResource("Insert_FB"); ?>" />
          <?php 
      // else Conditional region1
      } else { ?>
          <div class="KT_operations">
            <input type="submit" name="KT_Insert1" value="<?php echo NXT_getResource("Insert as new_FB"); ?>" onclick="nxt_form_insertasnew(this, 'id_material_cientifico')" />
          </div>
          <input type="submit" name="KT_Update1" value="<?php echo NXT_getResource("Update_FB"); ?>" />
          <input type="submit" name="KT_Delete1" value="<?php echo NXT_getResource("Delete_FB"); ?>" onclick="return confirm('<?php echo NXT_getResource("Are you sure?"); ?>');" />
          <?php }
      // endif Conditional region1
      ?>
          <input type="button" name="KT_Cancel1" value="<?php echo NXT_getResource("Cancel_FB"); ?>" onclick="return UNI_navigateCancel(event, 'includes/nxt/back.php')" />
        </div>
      </div>
    </form>
  </div>
  <br class="clearfixplain" />
</div>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($popula_autor);

mysql_free_result($popula_palavraChave);

mysql_free_result($autor_x_material_cientifico);

mysql_free_result($palavra_chave_x_material_cientifico);
?>
